<template>
	
	
	<div class="home-box" style="margin-left: 65px">
	  <el-form :inline="true" :model="PageForm">
	    <el-form-item label="部门名称">
	      <el-input v-model="PageForm.deptName" placeholder="部门名称" />
	    </el-form-item>
	    <el-form-item label="具体考核指标">
	      <el-input
	        v-model="PageForm.targetName"
	        placeholder="具体考核指标"
	        clearable
	      ></el-input>
	    </el-form-item>
	    <el-form-item>
	      <el-button @click="searchEvent" type="primary">查询 </el-button>
	    </el-form-item>
	    <el-form-item>
	      <el-button @click="update" type="primary">重置 </el-button>
	    </el-form-item>
	  </el-form>
	</div>
	
  <el-button
    style="margin-left: 65px"
    color="#626aef"
    plain
    icon="Upload"
    @click="handleUP1"
	
	v-auth="'导入部门业绩'"
  >
    导入部门业绩
  </el-button>
  
  
  <el-button type="success" color="#626aef" plain icon="Upload" @click="handleUP2"
  v-auth="'导入规则'"
  >导入规则</el-button>
  
  <el-button
    color="#626aef"
    style="margin-left: 10px"
    :dark="isDark"
    plain
    icon="Download"
    @click="handleDown1"
	
	v-auth="'导出部门业绩'"
    >导出部门业绩</el-button
  >
  
  
  <el-button
    color="#626aef"
    style="margin-left: 10px"
    :dark="isDark"
    plain
    icon="Download"
    @click="handleDown2"
	v-auth="'导出业绩规则'"
    >导出业绩规则</el-button
  >
  
  
  <el-button type="success" plain @click="handleFileAll"
  v-auth="'开始计算'"
  >开始计算</el-button>
  
  <el-tooltip class="item" effect="dark" content="刷新" placement="top">
    <el-button circle icon="Refresh" @click="update" />
  </el-tooltip>
  <el-table
    :data="DeptPerformanceList"
    border
    style="
      width: 95%;
      margin: 20px auto;
      border-radius: 8px;
      box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
    "
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
  >
    <el-table-column
      prop="deptName"
      label="部门"
    ></el-table-column>
	<el-table-column
	  prop="type"
	  label="计算方式"
	></el-table-column>
	<el-table-column
	  prop="target"
	  label="具体考核指标"
	></el-table-column>
	<el-table-column
     prop="percent"
     label="权重"
   ></el-table-column>
   <el-table-column
     prop="budget"
     label="年度预算"
   ></el-table-column>
   
	<el-table-column
      prop="complete"
      label="完成值"
    ></el-table-column>
	<el-table-column
      prop="ins"
      label="备注"
    ></el-table-column>
	<!-- <el-table-column
	  prop="grade1"
	  label="实际完成进度"
	  width="90"
	></el-table-column> -->
	
	<el-table-column label="实际完成进度">
	  <template #default="scope">
	    <div style="display: flex; align-items: center">
	      <span style="margin-left: 10px">{{ scope.row.grade1}}%</span>
	    </div>
	  </template>
	</el-table-column>
	
	<!-- <el-table-column
	  prop="grade2"
	  label="结算分数"
	  width="90"
	></el-table-column> -->
	
    <el-table-column label="最后更新时间">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updateTime}}</span>
        </div>
      </template>
    </el-table-column>
    <!-- <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button
        >
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column> -->
  </el-table>
  <pagination
    class="pagination"
    v-show="DeptPerformancePage.total > 0"
    :total="DeptPerformancePage.total"
    v-model:page="PageForm.page"
    v-model:limit="PageForm.page_size"
    @pagination="handlePagination"
  />
  <Dialog
    v-model="dialogFormVisible"
    :CadreList="CadreList"
    :close="close"
    :update="update"
    :selectContent="selectContent"
    :type="type"
    :title="title"
  ></Dialog>
  
  <UploadExcel
    v-model="upload1.open"
    :title="upload1.upLoadTitle"
    :url="importUrl1"
    :tipShow="true"
    @onSuccess="onSuccess"
    @onDownTemplate="onDownTemplate1"
	@onError="onError"
  ></UploadExcel>
  
 <UploadExcel
    v-model="upload2.open"
    :title="upload2.upLoadTitle"
    :url="importUrl2"
    :tipShow="true"
    @onSuccess="onSuccess"
    @onDownTemplate="onDownTemplate2"
  	@onError="onError"
  ></UploadExcel>
  
  
  
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
import { getDeptPerformancePage, deleteOneReward , fileAll,search} from "@/api/deptPerformance";
import UploadExcel from "@/components/SendAndDownload/index1";
import Dialog from "./components/rewardWageDialog";
import { ElMessage } from "element-plus";
import { getDeptPerformanceTemplate,getPerformanceRuleTemplate } from "@/api/template";
import {downLoadDeptPerformance,downLoadPerformanceRule} from "@/api/downExcel";


const PageForm = ref({
	deptName: "",
	targetName: "",
  page: 1,
  page_size: 10,
});
const DeptPerformancePage = ref([]);
const DeptPerformanceList = ref([]);

const searchEvent = () => {
  search(PageForm.value).then((data) => {
    DeptPerformancePage.value = data.data;
    DeptPerformanceList.value = data.data.records;
  });
};


onBeforeMount(() => {
  getDeptPerformancePage(PageForm.value).then((data) => {
    DeptPerformancePage.value = data.data;
    DeptPerformanceList.value = data.data.records;
  });
});
watch(DeptPerformanceList, (newVal) => {}, { deep: true });
const handlePagination = (value) => {
	if (PageForm.value.deptName == "" && PageForm.value.targetName == "") {
	  getDeptPerformancePage(PageForm.value).then((data) => {
	    DeptPerformancePage.value = data.data;
	    DeptPerformanceList.value = data.data.records;
	  });
	} else {
	  PageForm.value.page = value.page;
	  PageForm.value.page_size = value.page_size;
	  search(PageForm.value).then((data) => {
	    DeptPerformancePage.value = data.data;
	    DeptPerformanceList.value = data.data.records;
	  });
	}
	
};

const importUrl1 = ref("/auth/excel/deptPerformance/upload");
const importUrl2 = ref("/auth/excel/performanceRule/upload");
const upload1 = ref({
  open: false,
  upLoadTitle: "部门业绩上传",
});

const upload2 = ref({
  open: false,
  upLoadTitle: "部门业绩规则上传(无规则则按实际得分计算)",
});

const onSuccess = (res) => {
  getDeptPerformancePage(PageForm.value).then((data) => {
    DeptPerformancePage.value = data.data;
    DeptPerformanceList.value = data.data.records;
  });
};

const onError = (rep) =>{
	
}


const handleUP1 = () => {
  upload1.value.open = true;
};
const handleUP2 = () => {
  upload2.value.open = true;
};



const onDownTemplate1 = () => {
  getDeptPerformanceTemplate();
};

const onDownTemplate2 = () => {
  getPerformanceRuleTemplate();
};


const handleDown1 = (index, row) => {
  downLoadDeptPerformance().then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "导出成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("导出失败");
    }
  });
};


const handleDown2 = (index, row) => {
  downLoadPerformanceRule().then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "导出成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("导出失败");
    }
  });
};


const title = ref("新增部门业绩");
const type = ref("add");
const selectContent = ref();
const dialogFormVisible = ref(false);
const handleEdit = (index, row) => {
  selectContent.value = row;
  title.value = "编辑部门业绩";
  type.value = "edit";
  dialogFormVisible.value = true;
};


// const handleDelete = (index, row) => {
//   delete row.createTime;
//   delete row.updateTime;
//   row.id = row.empRewardId;
//   deleteOneReward(row).then((data) => {
//     if (data.code == 200) {
//       ElMessage({
//         message: "删除成功",
//         type: "success",
//       });
//       update();
//     } else {
//       ElMessage.error("删除失败");
//     }
//   });
// };

const close = () => {
  dialogFormVisible.value = false;
};

const update = () => {
	PageForm.value.deptName = "";
	
	PageForm.value.targetName = "";
	
  getDeptPerformancePage(PageForm.value).then((data) => {
    DeptPerformancePage.value = data.data;
    DeptPerformanceList.value = data.data.records;
  });
};

const handleFileAll=()=>{
  fileAll().then(data=>{
    if (data.code == 200) {
      ElMessage({
        message:"计算成功",
        type: "success",
      });
      update();
    } 
    else {
      ElMessage.error("计算失败");
    }
  })
}



</script>

<style lang="scss" scoped>
.title1 {
  color: rgb(64, 158, 255);
  width: 300px;
  height: 45px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 45px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>
